
import { _decorator, Component, Node, Sprite, UITransform, Animation, animation, AnimationClip, Vec3, SpriteFrame } from 'cc'
import { CONTROLLER_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, DIRECTION_ENUM, ENTITY_STATE_ENUM, DIRECTION_ORDER_ENUM, ENTITY_TYPE_ENUM } from '../../Enums'
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager'
import { ISpikes } from '../../Levels'
import { SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../../Enums'
import { SpikesStateMachine } from './SpikesStateMachine'
import { randomByLength } from '../../Utils'
import StateMachine from '../../Base/StateMachine'
import EventManager from '../../Runtime/EventManager'
import DataManager from '../../Runtime/DataManager'
const { ccclass, property } = _decorator

@ccclass('SpikesManager')
export class SpikesManager extends Component {
    id: string = randomByLength(12)
    x: number = 0
    y: number = 0
    fsm: StateMachine

    private _count: number
    private _totalCount: number
    type: ENTITY_TYPE_ENUM

    get count() {
        return this._count
    }

    set count(newCount) {
        this._count = newCount
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_CURRENT_COUNT, newCount)
    }

    get totalCount() {
        return this._totalCount
    }

    set totalCount(newCount) {
        this._totalCount = newCount
        this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT, newCount)
    }

    async init(params: ISpikes) {
        const sprite = this.addComponent(Sprite)
        sprite.sizeMode = Sprite.SizeMode.CUSTOM

        const transform = this.node.getComponent(UITransform)
        transform.setContentSize(TILE_WIDTH * 4, TILE_WIDTH * 4)

        this.fsm = this.addComponent(SpikesStateMachine)
        await this.fsm.init()

        this.x = params.x
        this.y = params.y
        this.type = params.type
        this.totalCount = SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[this.type as 'SPIKES_ONE' | 'SPIKES_TWO' | 'SPIKES_THREE' | 'SPIKES_FOUR']
        this.count = params.count

        this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)

        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop, this)
    }

    update() {
        // this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
    }

    onDestroy() {
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onLoop)
    }

    onLoop() { // 玩家每次移动后，尖刺计数器都+1
        if (this.count === this.totalCount) {
            this.count = 1
        } else {
            this.count++
        }

        this.onAttack()
    }

    backZero() {
        this.count = 0
    }

    onAttack() {
        if (!DataManager.Instance.player) return

        const { x: playerX, y: playerY } = DataManager.Instance.player
        if (this.x === playerX && this.y === playerY && this.count === this.totalCount) {
            EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.DEATH)
        }
    }
}
